from PIL import Image, ImageDraw, ImageColor

def draw_rounded_gradient_bar(
    score,
    width=400,  # 增大进度条宽度
    height=40,  # 增大进度条高度（整体放大）
    radius=20,  # 圆角半径与高度匹配，效果更明显
    start_color="#7B61FF",  # 深紫起始色
    end_color="#FF9BDB",    # 粉色结束色
    bg_color="#e7d9f3"      # 浅紫背景色
):
    score = max(score, 10)
    # 创建仅包含进度条的画布（尺寸与进度条完全贴合）
    img = Image.new("RGBA", (width, height), color=(0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    # 1. 绘制进度条背景（完整圆角，底层）
    bg_rect = (0, 0, width, height)
    draw.rounded_rectangle(bg_rect, radius=radius, fill=bg_color)
    # 计算填充宽度（确保有效范围）
    fill_width = int(score / 100 * width)
    if fill_width <= 0:
        return img
    fill_width = min(fill_width, width)  # 限制最大宽度不超过进度条总宽
    # 2. 填充区域（直角矩形，配合反向圆角偏移）
    # radius 是预留一块
    fill_rect = (radius, 0, fill_width-21, height)
    # 3. 绘制平滑渐变填充
    for x in range(fill_rect[0], fill_rect[2]):
        ratio = (x - fill_rect[0]) / fill_width
        r = int(ImageColor.getrgb(start_color)[0] + ratio * (ImageColor.getrgb(end_color)[0] - ImageColor.getrgb(start_color)[0]))
        g = int(ImageColor.getrgb(start_color)[1] + ratio * (ImageColor.getrgb(end_color)[1] - ImageColor.getrgb(start_color)[1]))
        b = int(ImageColor.getrgb(start_color)[2] + ratio * (ImageColor.getrgb(end_color)[2] - ImageColor.getrgb(start_color)[2]))
        tail_color = (r, g, b)
        draw.rectangle((x, fill_rect[1], x + 1, fill_rect[3]), fill=(r, g, b))
    # 4. 绘制头部反向圆角（左侧）
    head_radius = min(radius, fill_width)
    if head_radius > 0:
        draw.pieslice(
            (fill_rect[0]-radius, fill_rect[3] - 2*head_radius, fill_rect[0] + 2*head_radius-radius, fill_rect[3]),
            start=90, end=270,
            fill=start_color
        )
    tail_radius = min(radius, fill_width)
    if tail_radius > 0:
        draw.pieslice(
            (fill_rect[2] - 2*tail_radius + radius, fill_rect[3] - 2*tail_radius, fill_rect[2] + radius, fill_rect[3]),
            start=270, end=90,
            fill=tail_color
        )
    return img

# 示例调用（仅传入分数，无文字相关参数）
score = 0
img = draw_rounded_gradient_bar(score, width=600, height=40, radius=20)
img.save("img.png")